<?


	require_once '../init.php';

//takes precedense
	$images_id = intval($_REQUEST['images_id']);

	//incase images_id isn't passed
	$record_id = intval($_REQUEST['record_id']);
// 	$record_table = (table_exists($_REQUEST['record_table'])) ? $_REQUEST['record_table'] : 'unknown_table';
	$record_table = $_REQUEST['record_table'];


	$width = intval($_REQUEST['width']);
	$height = intval($_REQUEST['height']);

// 	if (empty($width) && !empty($height)) $width = $height;


	$sql = "SELECT mime, ";
	if (isset($_REQUEST['cropped'])) {
		$sql.= 'cropped';
	} elseif ($width != 0) {
		if ($width <= 360) $sql.= 'width_360';
		elseif ($width <= 720) $sql.= 'width_720';
		elseif ($width <= 1440) $sql.= 'width_1440';
		else $sql.= 'original';
	} elseif($height != 0) {
		if ($height <= 360) $sql.= 'width_360';
		elseif ($height <= 720) $sql.= 'width_720';
		elseif ($height <= 1440) $sql.= 'width_1440';
		else $sql.= 'original';
	} else $sql.= 'original';

	$sql.= " AS image FROM images WHERE";

  
	//first attempt to fetch image by images_id
	$qry = db_query("{$sql} id = $images_id LIMIT 1");

	//if no luck, then attempt to fetch by record_id and record_table -- assuming that the first image added is the desired image
	if (@db_num_rows($qry) == 0) $qry = db_query("{$sql} record_id = $record_id AND record_table = '$record_table' ORDER BY id ASC LIMIT 1");

// exit("{$sql} record_id = $record_id AND record_table = '$record_table' ORDER BY id ASC LIMIT 1");

	$image = (@db_num_rows($qry) == 1) ? @mysql_fetch_assoc($qry) : false;

	if (!$image || trim($image['image']) == '') {
		//try getting the original
		$sql = "SELECT mime, original AS image FROM images WHERE";
		$qry = db_query("{$sql} id = $images_id LIMIT 1");
		if (@db_num_rows($qry) == 0) $qry = db_query("{$sql} record_id = $record_id AND record_table = '$record_table' ORDER BY id ASC LIMIT 1");
		$image = (@db_num_rows($qry) == 1) ? mysql_fetch_assoc($qry) : false;
	}

/** testing ** /
	ob_end_clean();//clear out any framework html crap.
	header("Content-type: {$image_gd['mime']}");
	header('Content-Disposition: inline; filename="' . $image['filename'] . '"');
	exit($image['image']);
/** eof - testing **/

	if (!image || trim($image['image']) == '' || trim($image['mime']) == '') {
		$image = array();
		$module = module();
		$module_base = module_base();

		if (is_file('noimage.png')) {
			$image['image'] = file_get_contents('noimage.png');
			$image['mime'] = 'image/png';
		} elseif (is_file('noimage.gif')) {
			$image['image'] = file_get_contents('noimage.gif');
			$image['mime'] = 'image/gif';
		} elseif (is_file('noimage.jpg')) {
			$image['image'] = file_get_contents('noimage.jpg');
			$image['mime'] = 'image/jpeg';

		} elseif (is_file(LOCAL_PHPDRIVER . "/modules/$module_base/images/noimage.png")) {
			$image['image'] = file_get_contents(LOCAL_PHPDRIVER . "/modules/$module_base/images/noimage.png");
			$image['mime'] = 'image/png';
		} elseif (is_file(LOCAL_PHPDRIVER . "/modules/$module_base/images/noimage.gif")) {
			$image['image'] = file_get_contents(LOCAL_PHPDRIVER . "/modules/$module_base/images/noimage.gif");
			$image['mime'] = 'image/gif';
		} elseif (is_file(LOCAL_PHPDRIVER . "/modules/$module_base/images/noimage.jpg")) {
			$image['image'] = file_get_contents(LOCAL_PHPDRIVER . "/modules/$module_base/images/noimage.jpg");
			$image['mime'] = 'image/jpeg';

		} elseif (is_file(SHARED_PHPDRIVER . "/modules/$module_base/images/noimage.png")) {
			$image['image'] = file_get_contents(SHARED_PHPDRIVER . "/modules/$module_base/images/noimage.png");
			$image['mime'] = 'image/png';
		} elseif (is_file(SHARED_PHPDRIVER . "/modules/$module_base/images/noimage.gif")) {
			$image['image'] = file_get_contents(SHARED_PHPDRIVER . "/modules/$module_base/images/noimage.gif");
			$image['mime'] = 'image/gif';
		} elseif (is_file(SHARED_PHPDRIVER . "/modules/$module_base/images/noimage.jpg")) {
			$image['image'] = file_get_contents(SHARED_PHPDRIVER . "/modules/$module_base/images/noimage.jpg");
			$image['mime'] = 'image/jpeg';
		} else {
			$image['image'] = file_get_contents(SHARED_PHPDRIVER . 'pubhtml/images/noimage.gif');
			$image['file_name'] = "noimage.gif";
			$image['mime'] = 'image/gif';
		}
	} else $image['file_name'] = "images_{$record_table}_{$record_id}-{$images_id}.jpg";



	#exit(print_r($image, true));
	if (substr($image['file_name'], 0, 6) == 'noimage') {
	  $image_gd = $image;
	} else {
	  $image_gd = gd_img($image['image'], $width, $height, $image['mime']);
	}

  #exit(print_r($image_gd));
  
  //in case gd failed
  if (empty($image_gd['image'])) $image_gd['image'] = addslashes($image['image']);

	ob_end_clean();//clear out any framework html crap.
	header("Content-type: {$image_gd['mime']}");
	header('Content-Disposition: inline; filename="' . $image['filename'] . '"');
	exit(stripslashes($image_gd['image']));


